package com.campus.happy;

import java.io.IOException;
import java.net.MalformedURLException;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.select.Elements;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

public class InfoActivity extends Activity {
	TextView title;
	TextView addr;
	String iconurl;
	TextView call;
	String url;
	String addr_s;
	String call_s;
	String title_s;

	public void onCreate(Bundle b) {
		super.onCreate(b);
		LinearLayout layout = new LinearLayout(this);
		setContentView(layout);
		TextView title = new TextView(this);
		layout.addView(title);

		new Thread() {
			public void run() {
				try {
					go();
				} catch (MalformedURLException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		}.start();
	}

	void go() throws MalformedURLException, IOException {
		url=getIntent().getStringExtra("url");
	iconurl=getIntent().getStringExtra("icon");
		Document doc = Jsoup.parse(CookieHttp.get(
				url, "UTF-8"));
		String name = doc.select("span.shop").first().text();
		String addr = doc.select("span[itemprop$=street-address]")
				.attr("title");
		// String p=doc.select("p.desc").text();
		Elements call = doc.select("span[itemprop$=tel]");
		StringBuffer b = new StringBuffer();
		for (org.jsoup.nodes.Element e : call) {
			b.append(e.text() + " ");

		}
		String time = doc.select("p.info info-indent").select("span").text();

		System.out.println(name + addr + "\n" + b + time);
		title_s = name;
		addr_s = addr;
		call_s = b.toString();
		Handler handler = new Handler(Looper.getMainLooper());
		handler.post(new Runnable() {

			@Override
			public void run() {
				// TODO Auto-generated method stub
				Context context = InfoActivity.this;
				LinearLayout layout = new LinearLayout(context);
				setContentView(layout);
				layout.setOrientation(LinearLayout.VERTICAL);
				TextView title = new TextView(context);
				layout.addView(title);
				TextView addr = new TextView(context);
				layout.addView(addr);
				TextView call = new TextView(context);
				layout.addView(call);
				ImageView icon = new ImageView(context);
				
				icon.setTag(iconurl);
				LinearLayout.LayoutParams lp=new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,LinearLayout.LayoutParams.WRAP_CONTENT);
				layout.addView(icon,lp);
				Load.down(icon, iconurl);
				
				title.setText(title_s);
				addr.setText(addr_s);
				call.setText(call_s);
			}
		});

	}

	static void send(Activity a, String url,String  icon) {
		Intent i = new Intent(a, InfoActivity.class);
		i.putExtra("url", url);
		i.putExtra("icon", icon);
		a.startActivity(i);

	}
}
